<?php

class micro extends Module {
	
	public function __construct() {
		parent::__construct(__CLASS__);
		
		$this->extend('__micro_ajax');
	}
	
	public function __default() {
		return $this->getList();
	}
	
	public function user() {
		$oTypeUser = TypesController::getInstance()->getTypeByName('user');
		
		$userLogin = getRequest('param0');
		
		$sel = new Selection;
		$sel->addType($oTypeUser);
		$sel->addFilter('login', $userLogin);
		$sel->addLimit(1);
		
		$res = $sel->getList();
		
		if (!count($res[0])) {
			throw new NotFound;
		}
		
		$oUser = ObjectsController::getInstance()->getObject($oTypeUser, $res[0][0]);
		$this->setPageTitle(lang('user_header',__CLASS__) . $userLogin);
		
		return $this->getList(array('owner_id' => $res[0][0]));
	}
	
	public function tag() {
		return $this->getList(array('tags' => getRequest('param0')));
	}
	
	public function getTagsCloud() {
		$oTypeMicro = TypesController::getInstance()->getTypeByName('micro');
		
		$aTags = FDump::getCountObjectValues (array($oTypeMicro), 'tags', 40);
		
		$k = 35 / (log ($aTags['max'] - $aTags['min'] + 1.1));
		
		$res = array ();
		
		for ($i = 0; $i < 40; ++$i) {
			if (!isset ($aTags['values'][$i])) {
				break;
			}
			
			$size = log ($aTags['values'][$i]['cnt'] + 1 - $aTags['min']) * $k;
			$size = $size > 10 ? $size : 10;
			$size = $size < 35 ? $size : 35;
			
			$res[]['tag'] = array (
				'value' => $aTags['values'][$i]['value'],
				'cnt'   => $aTags['values'][$i]['cnt'],
				'size'  => $size
			);
		}
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('tags', $res));
		
		return $response;	
	}
	
	private function getList(array $aFilters = null, array $aPages = null, array $aResponseAttributes = null) {
		$oTypeMicro = TypesController::getInstance()->getTypeByName('micro');
		
		$sel = new Selection;
		$sel->addType($oTypeMicro);
		$sel->setOrder('id', false);
		
		if ($aFilters !== null) {
			foreach($aFilters as $fieldName => $value) {
				if (is_array ($value)) {
					$sel->addFilter ($fieldName, $value[0], $value[1]);
				} else {
					$sel->addFilter ($fieldName, $value);
				}
			}
		}
		
		if ($aPages === null) {
			$aPages = array ();
			$aPages['page'] = getRequest ('page', 'int');
			if ($aPages['page'] > 0) {
				$aPages['page']--;
			}
			$aPages['onpage'] = 20; //(int) Regedit::getInstance()->get ('//modules/blogs/posts_on_page');
		}
		$sel->addLimit ($aPages['page'] * $aPages['onpage'], $aPages['onpage']);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		$response = new ModuleResponse('micro/index');
		
		if ($aResponseAttributes !== null) {
			foreach ($aResponseAttributes as $attrName => $attrValue) {
				$response->setAttribute ($attrName, $attrValue);
			}
		}
		
		$response->addObjects($aObjects);
		
		$response->addObject (new Array2XML ('pages', array (
			'onpage' => $aPages['onpage'],
			'total' => $sel->getTotalCount (),
			'page' => ++$aPages['page']
		)));
		
		return $response;
	}
	
}